package com.aplink.generic.widgets;

import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.EditTextType;
import com.aplink.generic.constant.SqlDataType;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.EditTextSpec;
import com.aplink.generic.util.AndroidUIParser;
import com.aplink.generic.util.convertcount.GenericConverter;
import com.aplink.generic.util.convertcount.GenericFilter;
import com.aplink.generic.util.objectrule.ObjectBaseRule;

public class CustomEdittext extends BaseView {
    private String mContent;
    private String mDataObj;
    private final EditText mEditText;
    private EditTextType mInputType;
    private String mMappingName;
    private int mMaxLength;
    private EditTextSpec mEditTextSpecFormat;

    public CustomEdittext( GenericFragment dataEntryActivity,
                           ControlSpec controlSpec, View view ) {
        super( dataEntryActivity, controlSpec, view );
        this.mEditText = (EditText) view;
        mEditTextSpecFormat = (EditTextSpec) getControlSpec();
    }

    @Override
    protected void loadDataToView() {
        if ( ( this.mDataObj != null ) && ! this.mDataObj.isEmpty() ) {
            this.mContent = getHashPassedValues().get( this.mMappingName );
            this.mEditText.setText( ( this.mContent ) );
        }
    }

    @Override
    protected void doIfNotHashPassedValues() {
    }

    @Override
    protected void setEventListener() {
        final boolean isSearch = mEditTextSpecFormat.isSearch();
        if ( ( isSearch == true )
                && ( mEditTextSpecFormat.getSearchControl() != null ) ) {
            final int idSearchControl = AndroidUIParser
                    .getViewIdByName( mEditTextSpecFormat.getSearchControl() );
            if ( idSearchControl == - 1 ) {
                return;
            }
            mEditText.addTextChangedListener( mTextWatcher );
        }
    }

    @Override
    protected void initViewProperties() {
        final boolean isReadOnly = mEditTextSpecFormat.isReadOnly();
        if ( isReadOnly ) {
            this.mEditText.setKeyListener( null );
            this.mEditText.setEnabled( false );
            this.mEditText.setCursorVisible( false );
            this.mEditText.setFocusable( false );
            this.mEditText.setFocusableInTouchMode( false );
        }

        this.mDataObj = getControlSpec().getDataObject();
        this.mMappingName = getControlSpec().getMappingName();
        this.mInputType = mEditTextSpecFormat.getDataInputType();
        this.mEditText.setInputType( this.mInputType.getValue() );

        if ( this.mInputType.equals( EditTextType.PASSWORD ) ) {
            this.mEditText.setTransformationMethod( PasswordTransformationMethod
                    .getInstance() );
        }
        this.mMaxLength = mEditTextSpecFormat.getMaxLength();
        this.mEditText
                .setFilters( new InputFilter[]{ new InputFilter.LengthFilter(
                        this.mMaxLength ) } );

        if ( getControlSpec().hasBaseRule() ) {
            final ObjectBaseRule baseRule = getControlSpec()
                    .getObjectBaseRule();
            final boolean isEnable = baseRule.isSatisfiedCondition(
                    getPassedGenericObject(), - 1 );
            this.mEditText.setEnabled( isEnable );
        }

    }

    private TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged( CharSequence arg0, int arg1, int arg2,
                                   int arg3 ) {
            String searchCommand = arg0.toString() + ";"
                    + mEditTextSpecFormat.getSearchField();
            getDataEntryActivity().getCustomListView().filter( searchCommand );
        }

        @Override
        public void beforeTextChanged( CharSequence arg0, int arg1, int arg2,
                                       int arg3 ) {

        }

        @Override
        public void afterTextChanged( Editable arg0 ) {
            String contentData = mEditText.getText().toString();
            String searchCommand = contentData.toString() + ";"
                    + mEditTextSpecFormat.getSearchField();
            getDataEntryActivity().getCustomListView().filter( searchCommand );
        }
    };

    public EditText getEditText() {
        return this.mEditText;
    }

    @Override
    public GenericAttribute<?, ?> getAttribute() {
        String name = getControlSpec().getMappingName();
        SqlDataType dataType = GenericFilter
                .getDataTypeOfAttributeFromObjectPropMappingName(
                        getObjPropList(), name );
        String value = mEditText.getText().toString();
        return GenericConverter.getGenericAttribute( name, dataType, value );
    }
}
